import logging

from pyleo.lib.base import *
from pyleo.model.Resource import *
from pyleo.model.lfs_content import *
from paste.request import parse_querystring

log = logging.getLogger(__name__)

class EditorController(BaseController):

    def __common(self):
        ''' Provides the controlling of exactly what will be edited.
            Expect three cases:
                a) edit?page=thing
                b) draft?page=thing
                c) draft/blah
                d) upload?page=thing
            In case a, we edit thing (or if not logged in return to thing)
            In case b, we put up a default page at drafts if logged in else return to thing
            In case c, we edit blah (or if not logged in return to blah).
            In case d, we put up the upload page, with the default of thing as to where to attach the
            uploaded file ... we also intend to return to thing ...
            '''    
        qs=dict(parse_querystring(request.environ))
        try:
            c.path=qs['page']
        except:
            pass # c.path set in base controller
        if not c.loginStatus: 
            response.status_code=401
            c.content='<p> Not logged in. Return to %s</p>'%c.path
            return render('path')
        
        c.dirs,c.files=[],[]
        
    def index(self,internal=0):
        ''' Return an editor form for the target query sting '''
        #start by seeing if there is any material already at this path
        if not internal: self.__common()
        if c.path!=None:
            r=resource(c.path)
            rr=r.get()
            exists=r.exists()
        else: exists=0
        if exists:
            c.content=(r.content or '')
            c.title=rr['title']
            c.contentType=r.content_type
            c.targetpath=c.path
            c.allowComments=rr['allow_comments']
            c.allowTrackBacks=rr['allow_trackbacks']
            c.categories=''
        else: 
            c.content=''
            c.title=''
            if c.path: c.title=c.path.split('/')[-1]
            c.contentType=g.default_content
            c.targetpath=(c.path or '')
            c.allowComments='YES'
            c.allowTrackBacks='YES'
            c.categories=''
        return render('edit')
        
    def draft(self):
        ''' This is a special case of the walk the file system. Drafts can only be shown 
        when logged in, and we show a special page for drafts.'''
        self.__common()
        c.dirs,c.files=lfs_content(g.lfs,g.draft_prefix)
        #Two cases: 
        #  1 come to drafts from elsewhere, show an empty page after a directory listing
        #  2 editing an existing draft page
        c.directoryTitle='Drafts'
        c.action=g.home_href+'put'
        if not c.path.startswith(g.draft_prefix):
            # case 1
            c.path=None
        return self.index(1)
        
    def upload(self):
        self.__common()
        c.default_path=c.path+'/'+'XXX'
        return render('upload')
        
